from rest_framework.response import Response
from rest_framework.views import APIView

from apps.goods.models import SKU, GoodsCategory, SPU
from apps.meiduo_admin.serializers.skus import *
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from apps.meiduo_admin.utils import PageNum
from rest_framework.permissions import IsAdminUser,DjangoModelPermissions
class SKUModleViewSet(ModelViewSet):
    """SKU表数据视图集"""
    serializer_class = SKUModelSerializer
    pagination_class = PageNum

    # 添加权限
    # IsAdminUser 登录或者查看
    # DjangoModelPermissions 对于模型权限的验证
    permission_classes = [IsAdminUser, DjangoModelPermissions]


    # 搜索功能
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)

        return SKU.objects.all()

class SKUCategoriesView(ListAPIView):
    """三级分类显示"""
    queryset = GoodsCategory.objects.filter(subs=None)

    serializer_class = SKUCategorySerializer


class SPUListAPIView(ListAPIView):
    """SPU表展示"""
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer



from apps.goods.models import SPUSpecification
class GoodsSpecsAPIView(APIView):
    """规格及选项"""
    def get(self,request,spu_id):

        spec = SPUSpecification.objects.filter(spu_id=spu_id)

        slz = GoodsSpecsModelSerializer(spec,many=True)

        return Response(slz.data)
